// src/router/index.js

import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true }
  },
  {
    path: '/user/:id',
    name:'User',
    component: () => import('@/views/UserProfile.vue'),
    children: [
      { path: 'profile', component: () => import('@/components/Profile.vue') }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !isAuthenticated()) {
    next('/login')
  } else {
    next()
  }
})

export default router

function isAuthenticated() {
  return !!localStorage.getItem('token') // 假设 token 存在即认证通过
}